package cn.stylefeng.guns.modular.shop.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.shop.entity.Shop;
import cn.stylefeng.guns.modular.shop.mapper.ShopMapper;
import cn.stylefeng.guns.modular.shop.model.params.ShopParam;
import cn.stylefeng.guns.modular.shop.model.result.ShopResult;
import  cn.stylefeng.guns.modular.shop.service.ShopService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 店铺 服务实现类
 * </p>
 *
 * @author lauyang
 * @since 2020-04-18
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {

    @Override
    public void add(ShopParam param){
        Shop entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ShopParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ShopParam param){
        Shop oldEntity = getOldEntity(param);
        Shop newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ShopResult findBySpec(ShopParam param){
        return null;
    }

    @Override
    public List<ShopResult> findListBySpec(ShopParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ShopParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ShopParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Shop getOldEntity(ShopParam param) {
        return this.getById(getKey(param));
    }

    private Shop getEntity(ShopParam param) {
        Shop entity = new Shop();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
